3D Graphics in XNA Game Studio
Developing 3D graphics is
similar to creating real-time art. Even more impressive are the
detailed and interactive 3D graphics found in modern real-time computer
games. It is impressive when you compare it against computer graphics
of ten years ago. Console and PC games were limited to a fixed set of
hardware features, and resolutions were quite low and far from today’s
high resolution, multisampled, antialiased, and almost realistic images.
With all of the new fancy
hardware-dependent graphical improvements of the past decade also came
an increase in complexity when creating graphics for games. Developers
deal with many complexities, even when drawing simple graphics on the
screen.
XNA Game Studio has
attempted to limit these complexities in some cases and in others to
solve them all together. As discussed in previous chapters, the
capability to have a basic graphics window display a clear color is
trivial and requires only that you create a new project for one of the
supported platforms. To get to that point, using C++ with DirectX or
OpenGL can take hundreds of lines of code. The graphics APIs exposed by
XNA Game Studio are easy to use and often lead to the developer to fall
into the correct solution using them.
Note
If you have used XNA
Game Studio before, you will notice a number of API differences in XNA
Game Studio 4.0. A tremendous amount of work and energy went into
updating the graphics APIs to be even easier to use and to support the
new Windows Phone 7 platform.
In this chapter, we cover
the basics developers should know before diving into creating 3D
graphics. We start with defining 3D graphics and their makeup. Then, we
focus on some basic math that is required when working on 3D graphics.
Next, we cover the different stages that are part of the graphics
pipeline. We finish by covering the XNA Game Studio GraphicsAdapter and GraphicsDevice classes and drawing the first 3D primitives on the screen.
What Are 3D Graphics?
The answer to this question
might seem simple, but it might not be obvious to those who have no
experience creating 3D games or applications. In reality, 3D graphics
are just an illusion. They are flat 2D images on a computer monitor,
television screen, or phone display.
In the real world, your eyes
work together to create an image that contains visual perspective.
Objects appear to be smaller the farther they are away from you
physically. Imagine you are standing in the middle of perfectly
straight and flat railroad tracks. If you look down at your feet, the
rails appear to be some distance apart. As you let your eyes move
towards the horizon and look farther down the tracks, it appears that
the track rails become closer to each other the farther they are away
from you. Having two eyes adds to this depth perception because each
eye has a slightly different view. They help to see slightly different
angles of objects. This difference is even greater for objects near to
you. If you alternate closing each eye, you see objects move left and
right.
When you create 3D
graphics is perspective is an illusion. When you are working with 3D
graphics concepts, remember that you are really creating a 2D image
that is displayed on the screen. Many parts of the graphics pipeline
have nothing to do with 3D at all and work only on the pixel level. A
friend of mine once described to me a helpful way to look at developing
3D graphics games. He related creating 3D graphics to how great
painters create stunningly realistic pieces of art. Although to a
viewer standing still the painting can appear to represent real 3D
objects in the world, it is still just layers of paint on flat canvas.
They are illusions just like computer-generated 3D graphics.
So what are 3D graphics?
They are computer-generated images that give the appearance of depth
and perspective, but in reality, they are just flat 2D images when
displayed.
Makeup of a 3D Image
3D graphics
images are generated differently. At their core, they are a bunch of
triangles drawn on the screen. If this sounds simplistic, it is in
order to be fast. Modern graphics hardware can be so fast because the
types of operations it performs are limited when compared to
general-purpose processors used for CPUs in PCs. Graphics hardware has
dedicated hardware for these operations and are designed to process
millions of triangle and pixel operations.
Groups of triangles drawn on
the screen are commonly called geometry. These collections of geometry
can represent almost anything from a box, to a space ship, to a human
marine storming the beaches of Normandy.
Each triangle is made up of
three vertices. Each vertex represents one of the corners of the
triangle and must contain at least the position of itself. A vertex can
also contain other data such as color, texture coordinates, normals,
and so on.
Although triangles
determine the shape of what is displayed on the screen, there are many
factors that determine the final color of pixels the triangle covers on
the screen.
When you draw 2D sprites
in XNA Game Studio, you use the 3D graphics pipeline. In the past, 2D
graphics were drawn using a technique called blitting, which was
copying memory from a surface such as a texture to the display memory.
With today’s graphics cards, it is easier and faster to just draw
two-textured triangles when you want to draw a texture on the screen.
Because sprites are also 3D geometry, many of the topics in this
chapter are also applicable to them.